package com.bm.service.impl;

import cn.hutool.core.date.DateTime;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bm.Result.CodeEnum;
import com.bm.Vo.Repair.PetDetailVo;
import com.bm.exception.BmException;
import com.bm.mapper.EjyyCommunityInfoMapper;
import com.bm.mapper.EjyyPetVaccinateMapper;
import com.bm.mapper.EjyyWechatMpUserMapper;
import com.bm.model.EjyyCommunityInfo;
import com.bm.model.EjyyPet;
import com.bm.model.EjyyPetVaccinate;
import com.bm.model.EjyyWechatMpUser;
import com.bm.query.Pet.*;
import com.bm.service.EjyyPetService;
import com.bm.mapper.EjyyPetMapper;
import com.bm.util.DateUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;

import java.util.List;

/**
*宠物service层
*/
@Service
@Slf4j
public class EjyyPetServiceImpl extends ServiceImpl<EjyyPetMapper, EjyyPet>
implements EjyyPetService{

    @Autowired
    private EjyyPetMapper ejyyPetMapper;

    @Autowired
    private EjyyPetVaccinateMapper ejyyPetVaccinateMapper;

    @Autowired
    private EjyyCommunityInfoMapper ejyyCommunityInfoMapper;

    @Autowired
    private EjyyWechatMpUserMapper ejyyWechatMpUserMapper;


    @Override
    public  PageInfo<EjyyPet> petListByPage(PetQuery petQuery) {
        List<EjyyPet> ejyyPets = ejyyPetMapper.selectPetByPage();
        PageHelper.startPage(petQuery.getPageNum(),petQuery.getPageSize());
        PageInfo<EjyyPet> pageInfo = new PageInfo(ejyyPets);
        return pageInfo;
    }

    /**
     * 新增宠物信息
     * @param petQuery
     * @return
     */
    @Override
    @Transactional
    public boolean createPet(CreatePetQuery petQuery) {
        //非空判断
        if(ObjectUtils.isEmpty(petQuery)){
            throw new BmException(CodeEnum.SYSTEM_PARAMETER_IS_EMPTY);
        }
        //创建ejyypet对象
        EjyyPet ejyyPet = new EjyyPet();
        //复制属性
        BeanUtils.copyProperties(petQuery,ejyyPet);
        ejyyPet.setWechatMpUserId(petQuery.getUserId());
        //将日期转换成long的形式
        long dateLong = Long.parseLong(DateTime.now().toDateStr().replaceAll("-", ""));
        //将日期放入create
        ejyyPet.setCreatedAt(dateLong);
        //调用数据库新增数据
        int insert = ejyyPetMapper.insert(ejyyPet);
        if(insert <= 0){
            throw new BmException(CodeEnum.SYSTEM_ERROR);
        }
        LambdaQueryWrapper<EjyyPet> lqw = new LambdaQueryWrapper<>();
        lqw.orderByDesc(EjyyPet::getId);
        //新增成功 获取宠物id
        List<EjyyPet> ejyyPetsList = ejyyPetMapper.selectList(lqw);
        //将id放入对象中
        ejyyPet.setId(ejyyPetsList.get(0).getId());
        //创建宠物疫苗类
        EjyyPetVaccinate ejyyPetVaccinate = new EjyyPetVaccinate();
        //将共同属性复制过去
        BeanUtils.copyProperties(petQuery,ejyyPetVaccinate);
        //将宠物id放入
        ejyyPetVaccinate.setPetId(ejyyPet.getId());
        //将创建时间转成long类型放入
        ejyyPetVaccinate.setCreatedAt(dateLong);
        //操作数据库
        int vaccinateFlag = ejyyPetVaccinateMapper.insert(ejyyPetVaccinate);
        if(vaccinateFlag <= 0){
            throw new BmException(CodeEnum.SYSTEM_ERROR);
        }
        return true;
    }

    /**
     * 根据id查询宠物信息
     *
     * @param petDetailQuery
     * @return
     */
    @Override
    public PetDetailVo petDetailById(PetDetailQuery petDetailQuery) {
        //非空判断
        if(ObjectUtils.isEmpty(petDetailQuery)){
            throw new BmException(CodeEnum.SYSTEM_PARAMETER_IS_EMPTY);
        }
        //查询宠物信息
        EjyyPet ejyyPet = ejyyPetMapper.selectById(petDetailQuery.getId());
        //查询社区信息
        EjyyCommunityInfo communityInfo = ejyyCommunityInfoMapper.selectById(petDetailQuery.getCommunityId());
        if(ObjectUtils.isEmpty(communityInfo)){
            throw new BmException(CodeEnum.COMMUNITY_IS_EMPTY);
        }
        //查询微信用户信息
        EjyyWechatMpUser ejyyWechatMpUser = ejyyWechatMpUserMapper.selectById(ejyyPet.getWechatMpUserId());
        if(ObjectUtils.isEmpty(ejyyWechatMpUser)){
            throw new BmException(CodeEnum.WECHAT_USER_IS_EMPTY);
        }
        //封装对象
        PetDetailVo petDetailVo = new PetDetailVo();
        //复制宠物信息
        BeanUtils.copyProperties(ejyyPet,petDetailVo);
        //放入需要的数据
        petDetailVo.setCommunityName(communityInfo.getName());
        petDetailVo.setWeChatUserName(ejyyWechatMpUser.getRealName());
        return petDetailVo;
    }


    /**
     * 根据id更新宠物执照
     * @param id
     * @param petLicenseQuery
     * @return
     */
    @Override
    public boolean updatePetLicense(Long id, PetLicenseQuery petLicenseQuery) {
        //创建宠物对象
        EjyyPet ejyyPet = new EjyyPet();
        //复制信息
        BeanUtils.copyProperties(petLicenseQuery,ejyyPet);
        //放入id
        ejyyPet.setId(id);
        //进行查询
        int petCount = ejyyPetMapper.updateById(ejyyPet);
        //返回数据
        return petCount > 0;
    }

    /**
     * 新增狂犬疫苗信息
     *
     * @param id
     * @param petVaccinateQuery
     * @return
     */
    @Override
    public boolean InsertPetVaccinate(Long id, PetVaccinateQuery petVaccinateQuery) {
        //创建更新对象
        EjyyPetVaccinate ejyyPetVaccinate = new EjyyPetVaccinate();
        if(petVaccinateQuery.getVaccinatedAt() <= 0 || !StringUtils.hasText(petVaccinateQuery.getVaccineType())){
            throw new BmException(CodeEnum.SYSTEM_PARAMETER_IS_EMPTY);
        }
        //封装数据
        ejyyPetVaccinate.setVaccinatedAt(petVaccinateQuery.getVaccinatedAt());
        ejyyPetVaccinate.setVaccineType(petVaccinateQuery.getVaccineType());
        ejyyPetVaccinate.setPetId(id);
        //使用工具类创建当前时间的long形式
        ejyyPetVaccinate.setCreatedAt(DateUtils.dateConversion());
        int count = ejyyPetVaccinateMapper.insert(ejyyPetVaccinate);
        return count > 0;
    }


}
